/**
 * 
 */
package com.java.test.application;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java.test.application.dto.MGDto;
import com.java.test.platmanage.dto.GameListInfo;
import com.java.test.platmanage.service.PlatService;
import com.java.test.util.JsonUtil;

/**
 * @author Administrator
 *
 */
@Component
public class ReadJson implements ApplicationRunner {

	@Value(value = "classpath:mg_gameslist.json")
	private Resource resourcemg;

	@Value(value = "classpath:pt_gameslist.json")
	private Resource resourcept;

	@Value(value = "classpath:pk_gameslist.json")
	private Resource resourcepk;

	@Value(value = "classpath:self_gameslist.json")
	private Resource resourcezy;

	@Autowired
	PlatService platService;

	@Override
	public void run(ApplicationArguments args) throws Exception {

		List<String> datasource = platService.selectallGame();

		System.out.println("读取json文件=====");
		Resource resource = null;
		try {
			for (int i = 0; i < 4; i++) {

				if (i == 0) {
					resource = resourcemg;
				} else if (i == 1) {
					resource = resourcept;
				} else if (i == 2) {
					resource = resourcepk;
				} else if (i == 3) {
					resource = resourcezy;
				}
				BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
				StringBuffer message = new StringBuffer();
				String line = null;
				while ((line = br.readLine()) != null) {
					message.append(line);
				}
				String defaultString = message.toString();
				String result = defaultString.replace("\r\n", "").replaceAll(" +", "");

				JSONObject jsonObj = JSONObject.parseObject(result);

				List<MGDto> list = new ArrayList<MGDto>();
				for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
					MGDto info = JsonUtil.json2obj(entry.getValue().toString(), MGDto.class);
					list.add(info);
				}
				System.out.println("list的长度" + list.size());
				for (MGDto b : list) {
					GameListInfo binfo = new GameListInfo();
					binfo.setSort(0);
					binfo.setGameName(b.getGameName_en());
					binfo.setGameType(1);
					if (i == 0) {
						binfo.setPlatId(3);
					} else if (i == 1) {
						binfo.setPlatId(2);
					} else if (i == 2) {
						binfo.setPlatId(1);
					} else if (i == 3) {
						binfo.setPlatId(0);
					}
					binfo.setGameCode(b.getGame_Code());
					binfo.setFictitiousNumber(0);
					binfo.setFictitiousNumberMax(100);
					binfo.setOpenLanguage(b.getLanguage());
					binfo.setContent(b.getGameName_cn());

					JSONArray jsobj  = JSONArray.parseArray(b.getCurrency());	
					String hbstr="";
					for(int j=0;j<jsobj.size();j++) {
						hbstr+=jsobj.get(j)+",";
					}
					hbstr=hbstr.substring(0, hbstr.length()-1);

					binfo.setFoundName(hbstr);
					// 游戏状态开始禁用
					binfo.setStatus(0);
					if (datasource == null || datasource.size() <= 0) {
						platService.insertGameInfo(binfo);
					} else {
						if (!datasource.contains(binfo.getGameCode())) {
							platService.insertGameInfo(binfo);
						}
					}
				}

			}
			System.out.println("读取json文件END=====");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

	}

}
